package com.jyu.apts.service.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.jyu.apts.dao.ItemProductionLineRepository;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.service.ItemProductionLineService;

@Service
public class ItemProductionLineServiceImpl implements ItemProductionLineService {
	
	@Autowired
	ItemProductionLineRepository itemPrpductionLineDao;
	
	@Override
	public ItemProductionLine getOne(long id) {
		return itemPrpductionLineDao.findById(id).get();
	}

	@Override
	public Page<ItemProductionLine> getItemProductionLinePage(Integer pageNumber, Integer pageSize,Long organizationId, Long itemId, String search) {
		if(pageNumber == null || pageNumber <= 0) {
			pageNumber = 1;
		}
		if(pageSize == null || pageSize <= 0) {
			pageSize = 10;
		}
		Sort sort = new Sort(Sort.Direction.ASC,"id");
		PageRequest pageRequest = new PageRequest(pageNumber-1,pageSize,sort);
		Specification<ItemProductionLine> spec = new Specification<ItemProductionLine>() {
			@Override
			public Predicate toPredicate(Root<ItemProductionLine> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate pre = criteriaBuilder.conjunction();
				if(organizationId!=null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("item").get("organization").get("id"), organizationId.longValue()));
				}
				if(itemId != null) {
					pre = criteriaBuilder.and(pre,criteriaBuilder.equal(root.get("item").get("id"), itemId.longValue()));
				}
				if(search!=null && search.trim().length()!=0) {
					Predicate or_pre = criteriaBuilder.disjunction(); 
					or_pre =criteriaBuilder.or(or_pre,criteriaBuilder.like(root.get("name"), "%"+search+"%"));
					pre = criteriaBuilder.and(pre,or_pre);
				}
				return pre;
			}
			
		};
		return itemPrpductionLineDao.findAll(spec, pageRequest);
	}

	@Override
	public void save(ItemProductionLine itemPrpductionLine) {
		itemPrpductionLineDao.saveAndFlush(itemPrpductionLine);
	}

	@Override
	public void save(List<ItemProductionLine> itemPrpductionLines) {
		itemPrpductionLineDao.saveAll(itemPrpductionLines);
		itemPrpductionLineDao.flush();
	}

	@Override
	public void delete(List<ItemProductionLine> itemPrpductionLines) {
		itemPrpductionLineDao.deleteAll(itemPrpductionLines);
	}

	@Override
	public void delete(ItemProductionLine itemPrpductionLine) {
		itemPrpductionLineDao.delete(itemPrpductionLine);
	}

	@Override
	public List<ItemProductionLine> findAll() {
		return itemPrpductionLineDao.findAll();
	}

	@Override
	public List<ItemProductionLine> findAllById(List<Long> ids) {
		return itemPrpductionLineDao.findAllById(ids);
	}

}
